package cn.u313.listener;


import catcode.CatCodeUtil;
import cn.hutool.core.date.DateUtil;
import cn.u313.comm.utils.*;
import cn.u313.entry.UserEntry;
import cn.u313.model.Data;
import cn.u313.model.ExecuteModel;
import cn.u313.model.MusicModel;
import cn.u313.repository.YanYuEntryRepository;
import cn.u313.service.JiLuEntryService;
import cn.u313.service.UserEntryService;
import cn.u313.service.YanYuEntryService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.annotation.OnPrivate;
import love.forte.simbot.api.message.MessageContent;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.MsgSender;
import love.forte.simbot.filter.MatchType;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static cn.u313.comm.conststring.MoYuTxt.moGuanli;
import static cn.u313.config.BotConfig.hongBaos;

@Component
public class QQGroupListener extends BaseListen{
    @Autowired
    UserEntryService userEntryService;
    @Autowired
    JiLuEntryService jiLuEntryService;
    @Autowired
    YanYuEntryService yanYuEntryService;

    /**
     * 群签到 打卡
     * @param groupMsg
     * @param msgSender
     */
    @Async
    @OnGroup
    @Filter(value = "签到",matchType = MatchType.STARTS_WITH)
    @Filter(value = "打卡")
    public void onGroupMsg(GroupMsg groupMsg, MsgSender msgSender) {


        String qq = getQQ(groupMsg);
        String group = getGroup(groupMsg);
        String sendMsg = "";
        sendMsg =  userEntryService.qianDao(qq,group);
        if(sendMsg.contains("签到成功")){


        }
        sendMsg(msgSender,groupMsg,sendMsg);

    }


    /**
     * 打劫/抢劫
     * @param groupMsg
     * @param msgSender
     */
    @Async
    @OnGroup
    @Filter(value = "打劫" , matchType = MatchType.CONTAINS)
    @Filter(value = "抢劫" , matchType = MatchType.CONTAINS)
    public void dajie (GroupMsg groupMsg,MsgSender msgSender){

        String qq = getQQ(groupMsg);
        String qq1 = getQQ1(groupMsg);


        System.out.println(qq +">" +qq1);

        String sendMsg = userEntryService.daJie(qq,qq1,getGroup(groupMsg));
        sendMsg(msgSender,groupMsg,sendMsg);

    }
    /**
     * 查询
     * @param groupMsg
     * @param msgSender
     */
    @OnGroup
    @Filter(value = "查询" , matchType = MatchType.CONTAINS)
    public void from (GroupMsg groupMsg,MsgSender msgSender){
        if (groupMsg.getMsg().contains("粽子"))return;

        String qq = getQQ1(groupMsg);
        String sendMsg = "";
        if(qq==null)
            qq = getQQ(groupMsg);

        UserEntry u = userEntryService.getUser(qq);
        if(u==null)
            sendMsg = "用户【"+qq+"】查询失败！";
        else{
            sendMsg = "用户【"+qq+"】\n"+"悦币数量："+u.getM()+"\n红包余额："+u.getHongBao()+ "\nAi Token："+u.getGptToken();
        }
        sendMsg(msgSender,groupMsg,sendMsg);

    }
    @OnPrivate
    public void from (PrivateMsg groupMsg, MsgSender msgSender){


        System.out.println(groupMsg.getMsg());

    }
    @OnGroup
    @Filter(value = "抽红包")
    @Filter(value = "国庆红包")
    @Filter(value = "抽国庆红包")
    public void chouHongBao(GroupMsg groupMsg, MsgSender msgSender) {
            int yueb = 20;


            String qq = getQQ(groupMsg);

            String sendMsg= userEntryService.chouHongBao(qq,getGroup(groupMsg),yueb,hongBaos);
            sendMsg(msgSender,groupMsg,sendMsg);


    }
    @OnGroup
    @Filter(value = "抽会员",matchType =MatchType.STARTS_WITH )
    public void chouHy(GroupMsg groupMsg, MsgSender msgSender) {
        int yueb = 20;


        String qq = getQQ(groupMsg);

        String sendMsg= userEntryService.chouHy(qq,getGroup(groupMsg),yueb,hongBaos);
        sendMsg(msgSender,groupMsg,sendMsg);


    }


    @OnGroup
    @Filter(value = "排名")
    @Filter(value = "悦币排名")
    @Filter(value = "悦币排行")
    public void paihang(GroupMsg groupMsg, MsgSender msgSender) {

        String qq = getQQ(groupMsg);
        String group = getGroup(groupMsg);

        String sendMsg= userEntryService.paiHang(qq,group);
        sendMsg(msgSender,groupMsg,sendMsg);


    }
    @OnGroup
    @Filter(value = "领取",matchType =  MatchType.STARTS_WITH)
    @Filter(value = "礼包",matchType =  MatchType.STARTS_WITH)
    public void lingQu(GroupMsg groupMsg, MsgSender msgSender) {
        if(groupMsg.getMsg().contains("万能卡")){
            return;
        }
        String qq = getQQ(groupMsg);
        String group = getGroup(groupMsg);
        int n = getN(groupMsg);
        String sendMsg ="";
        if (n == 0) {
            sendMsg = userEntryService.getLingQu(qq,group);
        }else{
            sendMsg = userEntryService.lingQu(qq,n,group);
        }


        sendMsg(msgSender,groupMsg,sendMsg);


    }

    @OnGroup
    @Filter(value = "摸",matchType = MatchType.STARTS_WITH)
    public void 摸鱼(GroupMsg groupMsg, MsgSender msgSender) {
        if(groupMsg.getMsg().equals("摸鱼")){
            String qq = getQQ(groupMsg);
            String group = getGroup(groupMsg);

            String sendMsg= userEntryService.moYu(qq,group);
            sendMsg(msgSender,groupMsg,sendMsg);

        }else{
            sendMsg(msgSender,groupMsg,moGuanli[(int) (moGuanli.length * (Math.random()))]);
        }


    }
    @OnGroup
    @Filter(value = "赠送",matchType = MatchType.CONTAINS)
    public void zengsong(GroupMsg groupMsg, MsgSender msgSender) {
        if(groupMsg.getMsg().contains("福卡")||groupMsg.getMsg().contains("粽子"))return;
        String qq = getQQ(groupMsg);
        String qq1 = getAt(groupMsg);
        String group = getGroup(groupMsg);
        int n = getN(groupMsg);

        String sendMsg= userEntryService.zengsong(qq,qq1,n,group);
        sendMsg(msgSender,groupMsg,sendMsg);


    }

    public Map<String,String> duiHuan = new HashMap<>();
    @OnGroup
    @Filter(value = "兑换悦币",matchType = MatchType.STARTS_WITH)
    public void tiXian(GroupMsg groupMsg, MsgSender msgSender) {
        String qq = getQQ(groupMsg);
        if(!duiHuan.containsKey(qq))
            duiHuan.put(qq,"0");
        UserEntry userEntry = userEntryService.getUser(qq);
        String sendMsg= "你的红包余额为："+userEntry.getHongBao()+"\n可兑换成悦币数："+(int)(userEntry.getHongBao()*1000) +
                "\n继续操作请发送：红包【"+userEntry.getHongBao()+"】";
        sendMsg(msgSender,groupMsg,sendMsg);


    }
    @OnGroup
    @Filter(value = "红包",matchType = MatchType.STARTS_WITH)
    public void duihuanhongbao(GroupMsg groupMsg, MsgSender msgSender) {
        String qq = getQQ(groupMsg);

        double n = getDouble(groupMsg);
        String group = getGroup(groupMsg);
        if(!(n>0)&&(groupMsg.getMsg().equals("红包十连抽")||groupMsg.getMsg().equals("红包十连"))){
            int yueb = 20;
            if(Util.getTime(0) >20210930&&Util.getTime(0)<20211008){
                yueb = yueb/2;
            }

            String sendMsg = userEntryService.shiLianHongbao(qq,yueb,group);
            sendMsg(msgSender,groupMsg,sendMsg);
            return ;
        }
        if(!duiHuan.containsKey(qq))

            sendMsg(msgSender,groupMsg,"请先发送：兑换悦币\n查看自己余额后才能兑换");
        else {

           // double n = getDouble(groupMsg);
            String sendMsg = userEntryService.duiHuan(qq,n,group);
            sendMsg(msgSender,groupMsg,sendMsg);
            duiHuan.remove(qq);
        }



    }
//    @OnGroup
//    @Filter(value = "发放礼包",matchType = MatchType.STARTS_WITH)
//    public void faFangLiBao(GroupMsg groupMsg, MsgSender msgSender) {
////        String qq = getQQ(groupMsg);
////        if(!duiHuan.containsKey(qq))
////
////            sendMsg(msgSender,groupMsg,"请先发送：兑换悦币\n查看自己余额后才能兑换");
////        else {
////
////            String group = getGroup(groupMsg);
////            double n = getDouble(groupMsg);
////            String sendMsg = userEntryService.duiHuan(qq,n,group);
////            sendMsg(msgSender,groupMsg,sendMsg);
////            duiHuan.remove(qq);
////        }
//
//
//
//    }
    @OnGroup
    @Filter(value = "悦币",matchType = MatchType.STARTS_WITH)
    public void ybb(GroupMsg groupMsg, MsgSender msgSender) {
        String qq = getQQ(groupMsg);

        double n = getDouble(groupMsg);
        String group = getGroup(groupMsg);
        if(!(n>0)&&(groupMsg.getMsg().equals("悦币十连抽")||groupMsg.getMsg().equals("悦币十连"))){
            int yueb = 20;
            if(Util.getTime(0) >20210930&&Util.getTime(0)<20211008){
                yueb = yueb/2;
            }

            String sendMsg = userEntryService.shiLianYB(qq,yueb,group);
            sendMsg(msgSender,groupMsg,sendMsg);
            return ;
        }




    }
    @OnGroup
    @Filter(value = "发放",matchType = MatchType.STARTS_WITH)
    public void fafang(GroupMsg groupMsg, MsgSender msgSender) {
        if(!isManager(getQQ(groupMsg))){
            return ;
        }
        String txt = groupMsg.getMsg().length()>2?groupMsg.getMsg().substring(groupMsg.getMsg().indexOf("{")):null;
        String msg = "";
        if(txt!=null&&txt.length()>10){
            msg = userEntryService.sendJiangLi(txt,getGroup(groupMsg));
        }else{
            ExecuteModel e = new ExecuteModel();
            e.setContext("描述");
            e.setNum("0");
            e.setManagerQQ("1054980699");
            e.setType(0);
            e.setQq("QQ");
            msg = "发放奖励"+JSON.toJSONString(e);
        }

        sendMsg(msgSender,groupMsg,msg,false);



    }

    String urltg="https://cloud.qqshabi.cn/api/tiangou/api.php";
    @Filter(value = "舔狗日记")
    @OnGroup
    public void aas9(GroupMsg groupMsg, MsgSender msgSender){
        try {
//            if (!kouYueBi(groupMsg.getQQ(),5)) {
//
//                sendMsg(msgSender,groupMsg, "悦币不足，舔不起了哦" );
//                return;
//            }
//            String QQ = groupMsg.getQQ();
//            String msg = groupMsg.getMsg();
            String fun =   Http.sendGet(urltg);

            if (fun == null) {

                MsgUtil.sendMsg(msgSender,groupMsg, "[CAT:face,id=106]哎呀，开小差了");
                return;
            } else
                fun=fun.substring(11);
            MsgUtil.sendMsg(msgSender,groupMsg, Util.getDateString()+fun);
            //kouYueBi(QQ,5);

        } catch (Exception e) {
            MsgUtil.sendMsg(msgSender,groupMsg, "哎呀，开小差又被抓了\n" + "[CAT:face,id=106]");
        }
    }
    @OnGroup
    @Filter(value = "edg",matchType = MatchType.CONTAINS)
    @Filter(value = "EDG",matchType = MatchType.CONTAINS)
    public void eng(GroupMsg groupMsg, MsgSender msgSender) {
        sendMsg(msgSender,groupMsg,"恭喜EDG夺冠，EDG牛逼！");



    }
}
